package com.pgeneral.user.management.config;

import com.alibaba.fastjson.JSONArray;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ResultBean
 * Description 返回结果
 * @Author
 * @Date 2020/4/23 16:19
 * Version 1.0
 */
@Data
public class ResultBean<T> {
    private String code;
    private String message;
    private T data;

    private static ResultBean<Object> resultBean = new ResultBean<>();

    private ResultBean() {

    }

    public static ResultBean<Object> Error(String code, String message) {
        resultBean.setCode(code);
        resultBean.setMessage(message);
        return resultBean;
    }

    public static ResultBean<Object> Success() {
        resultBean.setCode("1000");
        resultBean.setMessage("Success");
        return resultBean;
    }

    public static ResultBean<Object> Success(Object data) {
        ResultBean<Object> resultBean = new ResultBean<>();
        resultBean.setCode("1000");
        resultBean.setMessage("Success");
        resultBean.setData(JSONArray.toJSON(data));
        return resultBean;
    }

    public static ResultBean GetResult(boolean success) {
        if (success) {
            resultBean.setCode("1000");
            resultBean.setMessage("Success");
        } else {
            resultBean.setCode("1001");
            resultBean.setMessage("False");
        }
        return resultBean;
    }

    public static ResultBean<Map<String, Integer>> InsertSuccessResult(Integer insertCount) {
        ResultBean<Map<String, Integer>> resultBean = new ResultBean<>();
        Map<String, Integer> map = new HashMap<>();
        if (insertCount >= 0) {
            resultBean.setCode("1000");
            resultBean.setMessage("Success");
        } else {
            resultBean.setCode("1001");
            resultBean.setMessage("False");
        }
        map.put("count", insertCount);
        resultBean.setData(map);
        return resultBean;
    }
    public static ResultBean<Object> Success(String insertCount,String message) {
        ResultBean<Object> resultBean = new ResultBean<>();
        resultBean.setCode(insertCount);
        resultBean.setMessage(message);
        return resultBean;
    }
}